﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace QuangMobile
{
    public partial class ProductList : System.Web.UI.Page
    {
        private static int cID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["cID"] != null)
            {
                cID = Convert.ToInt32(Request.QueryString["cID"]);
            }
            LoadSanPham();
        }

        private void LoadSanPham()
        {
            string sql = "SELECT TenDM FROM dbo.tblWebDanhMuc WHERE ID= @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, cID));
            ProductCategory.Text = SqlHelper.ExecuteScalar(CommandType.Text, sql, arrParams).ToString();
            sql = "SELECT * FROM dbo.tblWebSanPham WHERE HangID_DM = @ID ORDER BY UpdateDate DESC";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UCProductItem Items = (UCProductItem)LoadControl("UCProductItem.ascx");
                UCBreak Break = (UCBreak)LoadControl("UCBreak.ascx");
                Items.ProductID = Convert.ToInt32(dt.Rows[i]["ID"]);
                if (dt.Rows[i]["AnhSP"] == DBNull.Value)
                {
                    Items.ProductImage = "../upload/no-image.png";
                }
                else
                {
                    Items.ProductImage = dt.Rows[i]["AnhSP"].ToString();
                }
                Items.ProductTitle = dt.Rows[i]["TenSP"].ToString();
                Items.ProductPrice = Convert.ToDouble(dt.Rows[i]["GiaSP"]);
                Items.ProductComment = dt.Rows[i]["MoTa"].ToString();
                ProductListCategory.Controls.Add(Items);
                if (i > 0 && i % 4 == 3)
                {
                    ProductListCategory.Controls.Add(Break);
                }
            }

        }
    }
}
